
$NG.AllReady(
    //表头页面逻辑
    function (page, { useAction, useDataIndexChange, useBeforeClick,useUpdateRow, useUpdateRows }) {
        const mstform = $NG.getCmpApi("p_form_pc_stakeholders_m");
        const dgrid = $NG.getCmpApi("p_form_pc_stakeholders_d");
        console.log("page is ready");
        console.log(mstform.getItem('u_sjbh').getValue());
        console.log(mstform.getItem('u_opportunity').getValue());

        $NG.updateUI((updater)=>{
            updater.form.p_form_pc_stakeholders_m.u_phidkey.setProps({
                hidden:true
            })
        })

        if (page.oType == "add") {
            $NG.updateState((updater)=>{
                updater.data.p_form_pc_stakeholders_m.setProps({
                    u_phidkey:new Date().getTime()
                })
            })
            mstform.setReadOnly("u_sjbh");
            mstform.setReadOnly("u_opportunity");
        }
        if (page.oType == "edit") {
            mstform.setReadOnly("phid_pc");

        }


    // useBeforeClick(async (prop) => {
	// 	const phidkey = mstform.getItem("u_phidkey").getValue();
	// 	const resBody = await $NG.request.get({
    //         url: "http://nginx.clouddev.hisense.com/sd/sdToCrm/apply/stakeholders/"+phidkey,
    //         header: {
    //             "Content-Type": "application/json",
    //         },
    //     });
    //     console.log(`后台接口接收信息`,resBody);
    //     return false
	// }, "save");

    useBeforeClick(()=>{
        const row = dgrid.getData()
        const error = row.reduce((value,item,index)=>{
            if(item.u_source == 'SD' && !item.u_mobile_phone){
                value.push(index+1)
            }
            return value
        },[])
        if(error.length != 0 ){
            $NG.alert(`干系人明细，第 ${error.join("、")} 行 来源类型为SD，手机为必输项`)
            return false
        }
        return true
    },"save")

        if (dgrid) {
            // 实现对表体单元格设置必输和只读
            dgrid.subscribe(({ dataIndex, row }) => {
                // 排除列表
                const confirmKey = ["u_zt"]
                var u_source = "SD";
                u_source = dgrid.getRows()[dgrid.getCheckedIndexes()[0]].u_source;
                if(confirmKey.includes(dataIndex)) return 
                if (u_source != "SD") {
                    dgrid.endEditing();
                    $NG.alert("只有干系人来源为SD的项目干系人才能进行编辑");
                }
            }, "startEditing");

            // dgrid.subscribe(({ dataIndex, row }) => {
            //     console.log(dataIndex, row);
            //     var u_source = "SD";
            //     u_source = dgrid.getRows()[dgrid.getCheckedIndexes()[0]].u_source;
            //     if (u_source != "SD") {
            //         dgrid.endEditing();
            //         $NG.alert("只有干系人来源为SD的项目干系人才能进行编辑");
            //     }
            // }, "onCheckedChange");
        }
        
        // const arrKey = ["u_identity","u_sjbh","u_opp_name","u_gxr_xm","u_mobile_phone","u_customer_name","u_dept","u_position","u_sex","u_level","u_business_gxcsq","u_is_stakeholder","u_primary_responseible_persion","u_zt","u_source","","","",]

        useUpdateRows(({ args }) => {
            const _arr = dgrid.getRows()
            _arr.forEach(element => {
                if (element.u_source == "" || element.u_source == undefined) {
                    element.u_source = "SD"
                }
            });
            dgrid.refreshView()
        }, "p_form_pc_stakeholders_d")
    },
    //编辑页面逻辑
    //  function (editPage, { useAction }) {
    //   console.log("editPage is ready");
    //   const listToolbar = $NG.getCmpApi("toolbar");
    //    listToolbar.getItem("add").setHidden();
    //   },
    //列表页面逻辑
    function (listPage, { useAction }) {
        console.log("listPage is ready");
    }
);
